package com.internship.javawebinternship.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.internship.javawebinternship.entity.Account;
import com.internship.javawebinternship.entity.DailyReport;
import com.internship.javawebinternship.myUtils.GetLocation;
import com.internship.javawebinternship.myUtils.ListToExcel;
import com.internship.javawebinternship.repository.AccountRepository;
import com.internship.javawebinternship.repository.DailyReportRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.http.HttpResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/dailyReport")
public class DailyReportController {

    AccountController accountController = new AccountController();
    @Autowired
    private DailyReportRepository dailyReportRepository;
    @Autowired
    private AccountRepository accountRepository;

    @PostMapping("showLastSubmit")//获取用户上次打卡数据，实现部分数据自动填写
    public Object showLastSubmit(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Object res = dailyReportRepository.findByStuempnoOrderByDateDesc(no);

        if (!((ArrayList<?>) res).isEmpty()) {

            Date DATE = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//将日期精确到具体的时分秒
            String date = formatter.format(DATE);
            String temp = ((DailyReport) ((ArrayList<?>) res).get(0)).getTemp();
//            String location = ((DailyReport) ((ArrayList<?>) res).get(0)).getLocation();

            GetLocation getLocation = new GetLocation();
            getLocation.location(request);

            String province = (String) session.getAttribute("province");
            String city = (String) session.getAttribute("city");
            String district = (String) session.getAttribute("district");

            session.removeAttribute("province");
            session.removeAttribute("city");
            session.removeAttribute("district");

            result.put("result", "1");
            result.put("no", no);
            result.put("date", date);
            result.put("temp", temp);
            result.put("province", province);
            result.put("city", city);
            result.put("district", district);

//            result.put("isfever", String.valueOf(isfever));
//            result.put("iscough", String.valueOf(iscough));
        } else {
            result.put("result", "0");
        }
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("showAllSubmit")//管理员查询所有的健康打卡记录，按日期降序，展示所有数据
    public Object showAllSubmit(
            //@RequestBody String frontEnd,
            HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();

        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (session.getAttribute("authority").equals("2") || session.getAttribute("authority").equals("3")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

//        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
//        if (receiveJSON.size() == 0) {
//            result.put("result", "0");
//            result.put("msg", "参数错误");
//            JSONObject object = new JSONObject(result);
//            return object;
//        }

        Map<String, Object> result2 = new HashMap<>();

        Object res = dailyReportRepository.showAllSubmit();
        Object[] obj = new Object[((ArrayList<?>) res).size()];
        if (!((ArrayList<?>) res).isEmpty()) {
            try {
                for (int i = 0; i < ((ArrayList<?>) res).size(); i++) {
                    DailyReport dailyReport = dailyReportRepository.showAllSubmit().get(i);
                    String no = dailyReport.getStuempno();
                    String name = accountRepository.findByStuempno(no).get(0).getName();

                    result.clear();
                    result.put("id", dailyReport.getReportid());
                    result.put("stuempno", dailyReport.getStuempno());
                    result.put("name", name);
                    result.put("date", dailyReport.getDate());
                    result.put("period", dailyReport.getPeriod());
                    result.put("temp", dailyReport.getTemp());
                    result.put("location", dailyReport.getLocation());
                    result.put("isfever", dailyReport.getIsfever());
                    result.put("iscough", dailyReport.getIscough());

                    result2.put("result", "1");
                    obj[i] = JSON.toJSON(result);
                }
            } catch (Exception e) {
                result2.clear();
                result2.put("result", "0");
                JSONObject object = new JSONObject(result2);
                return object;
            }
        } else {
            result2.clear();
            result2.put("result", 0);
            result2.put("msg", "似乎查询不到数据");
            JSONObject object = new JSONObject(result2);
            return object;
        }
        result2.put("data", obj);
        JSONObject object = new JSONObject(result2);
        return object;
    }

    @PostMapping("showPartSubmit")//辅导员查询其权限范围内的学生打卡记录
    public Object showPartSubmit(HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();

        HttpSession session = request.getSession(false);
        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (session.getAttribute("authority").equals("2") || session.getAttribute("authority").equals("3")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String empNo = (String) session.getAttribute("no");
//        int page = receiveJSON.getIntValue("page");
//        int num = receiveJSON.getIntValue("num");

        Map<String, Object> result2 = new HashMap<>();

        Object res = dailyReportRepository.showPartSubmit(empNo);
        Object[] obj = new Object[((ArrayList<?>) res).size()];
        if (!((ArrayList<?>) res).isEmpty()) {
            //int j = 0;
            try {
                for (int i = 0; i < ((ArrayList<?>) res).size(); i++) {
                    DailyReport dailyReport = dailyReportRepository.showPartSubmit(empNo).get(i);
                    String no = dailyReport.getStuempno();
                    String name = accountRepository.findByStuempno(no).get(0).getName();

                    result.clear();
                    result.put("id", dailyReport.getReportid());
                    result.put("stuempno", dailyReport.getStuempno());
                    result.put("name", name);
                    result.put("date", dailyReport.getDate());
                    result.put("period", dailyReport.getPeriod());
                    result.put("temp", dailyReport.getTemp());
                    result.put("location", dailyReport.getLocation());
                    result.put("isfever", dailyReport.getIsfever());
                    result.put("iscough", dailyReport.getIscough());

                    result2.put("result", "1");
                    obj[i] = JSON.toJSON(result);

                }
            } catch (Exception e) {
                result2.clear();
                result2.put("result", "0");
                JSONObject object = new JSONObject(result2);
                return object;
            }
        } else {
            result2.clear();
            result2.put("result", 0);
            result2.put("msg", "似乎查询不到你的学生的数据");
            JSONObject object = new JSONObject(result2);
            return object;
        }
        result2.put("data", obj);
        JSONObject object = new JSONObject(result2);
        return object;
    }


    @PostMapping("submitDailyReport")//用户每日健康打卡
    public Object submitDailyReport(HttpServletRequest request, @RequestBody String frontEnd) {

        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fdate = formatter.format(date);
        String period = receiveJSON.getString("period");
        String temp = receiveJSON.getString("temp");
        String location = receiveJSON.getString("location");
        String isfever = receiveJSON.getString("isfever");
        String iscough = receiveJSON.getString("iscough");

        DailyReport dailyReportFromDB = new DailyReport();
        Account account = accountRepository.findByStuempno(no).get(0);
        String name = account.getName();

        try {
            dailyReportFromDB.setStuempno(no);
            dailyReportFromDB.setDate(fdate);
            dailyReportFromDB.setPeriod(period);
            dailyReportFromDB.setTemp(temp);
            dailyReportFromDB.setLocation(location);
            dailyReportFromDB.setIsfever(isfever);
            dailyReportFromDB.setIscough(iscough);
            dailyReportRepository.save(dailyReportFromDB);
            result.put("result", "1");
            result.put("msg", name + "，您已成功提交" + fdate + "的健康打卡报告");
        } catch (Exception e) {
            result.put("result", "0");
            result.put("msg", "提交失败，请重试");
            e.printStackTrace();
        }
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("showSubmitByno")//学生端查看自己的健康打卡
    public Object showSubmitByno(HttpServletRequest request) {

        HttpSession session = request.getSession(false);

        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

//        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
//        if (receiveJSON.size() == 0) {
//            result.put("result", "0");
//            result.put("msg", "参数错误");
//            JSONObject object = new JSONObject(result);
//            return object;
//        }
//        int page = receiveJSON.getIntValue("page");
//        int num = receiveJSON.getIntValue("num");

        Map<String, Object> result2 = new HashMap<>();
        Object res = dailyReportRepository.findByStuempnoOrderByDateDesc(no);
        Object[] obj = new Object[((ArrayList<?>) res).size()];
        if (!((ArrayList<?>) res).isEmpty()) {
            try {
                for (int i = 0; i < ((ArrayList<?>) res).size(); i++) {
                    DailyReport dailyReport = dailyReportRepository.findByStuempnoOrderByDateDesc(no).get(i);
                    String name = accountRepository.findByStuempno(no).get(0).getName();

                    result.clear();
                    result.put("id", dailyReport.getReportid());
                    result.put("no", dailyReport.getStuempno());
                    result.put("name", name);
                    result.put("date", dailyReport.getDate());
                    result.put("period", dailyReport.getPeriod());
                    result.put("temp", dailyReport.getTemp());
                    result.put("location", dailyReport.getLocation());
                    result.put("isfever", dailyReport.getIsfever());
                    result.put("iscough", dailyReport.getIscough());

                    result2.put("result", "1");
                    obj[i] = JSON.toJSON(result);
                }
            } catch (Exception e) {
                result2.clear();
                result2.put("result", "0");
                JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
                return object;//返回json数据给前端
            }
        } else {
            result2.put("result", "0");
            result2.put("msg", "你还没有健康打卡数据");
            JSONObject object = new JSONObject(result2);
            return object;
        }
        result2.put("data", obj);
        JSONObject object = new JSONObject(result2);
        return object;
    }

    @PostMapping("showSubmit")//(管理员/辅导员)根据日期查询 打卡数据
    public Object showSubmit(@RequestBody String frontEnd, HttpServletRequest request) {

        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (session.getAttribute("authority").equals("2") || session.getAttribute("authority").equals("3")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String date = receiveJSON.getString("date");
//        int page = receiveJSON.getIntValue("page");
//        int num = receiveJSON.getIntValue("num");

        Map<String, Object> result2 = new HashMap<>();
        Object res = dailyReportRepository.findByDateOrderByStuempnoDesc(date);
        Object[] obj = new Object[((ArrayList<?>) res).size()];
        try {
            for (int i = 0; i < ((ArrayList<?>) res).size(); i++) {
                DailyReport dailyReport = dailyReportRepository.findByDateOrderByStuempnoDesc(date).get(i);
                String no = dailyReport.getStuempno();
                String name = accountRepository.findByStuempno(no).get(0).getName();

                result.clear();
                result.put("ID", dailyReport.getReportid());
                result.put("no", dailyReport.getStuempno());
                result.put("name", name);
                result.put("date", dailyReport.getDate());
                result.put("period", dailyReport.getPeriod());
                result.put("temp", dailyReport.getTemp());
                result.put("location", dailyReport.getLocation());
                result.put("isfever", dailyReport.getIsfever());
                result.put("iscough", dailyReport.getIscough());

                result2.put("result", "1");
                obj[i] = JSON.toJSON(result);
            }
        } catch (Exception e) {
            result2.clear();
            result2.put("result", "0");
            JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }

        result2.put("data", obj);
        JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("showSubmitByMul")//多条件查询
    public Object showSubmitByMul(@RequestBody String frontEnd, HttpServletRequest request) {

        Map<String, Object> result = new HashMap<>();
        HttpSession session = request.getSession(false);

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (session.getAttribute("authority").equals("2") || session.getAttribute("authority").equals("3")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }
        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String isfever = receiveJSON.getString("isfever");
        String iscough = receiveJSON.getString("iscough");
        String temp = receiveJSON.getString("temp");

        session.setAttribute("isfever", isfever);
        session.setAttribute("iscough", iscough);
        session.setAttribute("temp", temp);

        Map<String, Object> result2 = new HashMap<>();

        Object res = dailyReportRepository.showSubmitByMul(isfever, iscough, temp);
        Object[] obj = new Object[((ArrayList<?>) res).size()];
        try {
            for (int i = 0; i < ((ArrayList<?>) res).size(); i++) {
                DailyReport dailyReport = dailyReportRepository.showSubmitByMul(isfever, iscough, temp).get(i);
                String no = dailyReport.getStuempno();
                String name = accountRepository.findByStuempno(no).get(0).getName();

                result.clear();
                result.put("id", dailyReport.getReportid());
                result.put("stuempno", dailyReport.getStuempno());
                result.put("name", name);
                result.put("date", dailyReport.getDate());
                result.put("period", dailyReport.getPeriod());
                result.put("temp", dailyReport.getTemp());
                result.put("location", dailyReport.getLocation());
                result.put("isfever", dailyReport.getIsfever());
                result.put("iscough", dailyReport.getIscough());

                result2.put("result", "1");
                obj[i] = JSON.toJSON(result);
            }
        } catch (Exception e) {
            result2.clear();
            result2.put("result", "0");
            JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
            return object;//返回json数据给前端
        }
        result2.put("data", obj);
        JSONObject object = new JSONObject(result2);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("getSubmit")//获取打卡数据
    public Object showModify(@RequestBody String frontEnd, HttpServletRequest request) {

        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String reportId = receiveJSON.getString("reportId");
        session.setAttribute("reportId", reportId);

        DailyReport dailyReport = dailyReportRepository.findByReportid(reportId).get(0);
        //获取 打卡数据
        String stuempno = dailyReport.getStuempno();
        //根据学号找到学生名字
        String name = accountRepository.findByStuempno(stuempno).get(0).getName();

        String date = dailyReport.getDate();
        String period = dailyReport.getPeriod();
        String temp = dailyReport.getTemp();
        String location = dailyReport.getLocation();
        String isfever = dailyReport.getIsfever();
        String iscough = dailyReport.getIscough();

        //将打卡数据 存在map集合中
        result.put("result", "1");
        result.put("stuempno", stuempno);
        result.put("name", name);
        result.put("date", date);
        result.put("period", period);
        result.put("temp", temp);
        result.put("location", location);
        result.put("isfever", isfever);
        result.put("iscough", iscough);

        //将map集合转换为json，提交给前端
        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("renewSubmit")//修改打卡数据
    public Object renewSubmit(HttpServletRequest request, @RequestBody String frontEnd) {

        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String no;
        try {
            no = (String) session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String isfever = receiveJSON.getString("isfever");
        String iscough = receiveJSON.getString("iscough");
        String temp = receiveJSON.getString("temp");
        String reportId = (String) session.getAttribute("reportId");
        session.removeAttribute("reportId");
        String date = receiveJSON.getString("date");
        String period = receiveJSON.getString("period");
        String location = receiveJSON.getString("location");

        DailyReport dailyReportRenew = dailyReportRepository.findByReportid(reportId).get(0);
        Account account = accountRepository.findByStuempno(no).get(0);
        String name = account.getName();
        try {
            dailyReportRenew.setStuempno(no);
            dailyReportRenew.setDate(date);
            dailyReportRenew.setPeriod(period);
            dailyReportRenew.setTemp(temp);
            dailyReportRenew.setLocation(location);
            dailyReportRenew.setIsfever(isfever);
            dailyReportRenew.setIscough(iscough);
            dailyReportRepository.save(dailyReportRenew);

            result.put("result", "1");
            result.put("msg", name + ",恭喜您已修改该健康打卡数据");

        } catch (Exception e) {
            result.put("result", 0);
            result.put("msg", "糟糕，遇到异常修改失败~");
            JSONObject object = new JSONObject(result);
            return object;
        }
        JSONObject object = new JSONObject(result);//使用JSONObject，将map转化为json数据
        return object;//返回json数据给前端
    }

    @PostMapping("deleteSubmit")//根据健康打卡的 id删除
    public Object deleteSubmit(@RequestBody String frontEnd, HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        JSONObject receiveJSON = JSONObject.parseObject(frontEnd);
        if (receiveJSON.size() == 0) {
            result.put("result", "0");
            result.put("msg", "参数错误");
            JSONObject object = new JSONObject(result);
            return object;
        }
        String reportId = receiveJSON.getString("reportId");

        int flag = dailyReportRepository.deleteByReportid(reportId);
        if (!(flag == 0)) {
            result.put("result", "1");
            result.put("msg", "删除操作成功");
        } else {
            result.put("result", "0");
            result.put("msg", "操作有误，删除失败");
        }
        JSONObject object = new JSONObject(result);
        return object;
    }

    @PostMapping("downloadExcelAfterFiltering")
    public Object downloadExcelAfterFiltering(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(false);
        Map<String, Object> result = new HashMap<>();

        if (forceLogOut(request)) {
            result.put("result", "-1");
            result.put("msg", "您的账号已在其他地方登录，您已被强制下线。若需要继续操作，请重新登录并注意账号安全");
            JSONObject object = new JSONObject(result);
            return object;
        }

        try {
            if (session.getAttribute("authority").equals("2") || session.getAttribute("authority").equals("3")) {
                result.put("result", "0");
                result.put("msg", "很抱歉，您无权限进行本操作，请联系管理员以获得帮助");
                JSONObject object = new JSONObject(result);
                return object;
            }
            session.getAttribute("no");
        } catch (NullPointerException e) {
            result.put("result", "0");
            result.put("msg", "您似乎尚未登录，请登录后重试");
            JSONObject object = new JSONObject(result);
            return object;
        }

        String isfever = String.valueOf(session.getAttribute("isfever"));
        String iscough = String.valueOf(session.getAttribute("iscough"));
        String temp = (String) session.getAttribute("temp");

        session.removeAttribute("isfever");
        session.removeAttribute("iscough");
        session.removeAttribute("temp");

        Date DATE = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");//将日期精确到具体的时分秒
        String date = formatter.format(DATE);

        String excelPath = ListToExcel.createExcel(dailyReportRepository.showSubmitByMul(isfever, iscough, temp), date);
        try {
            if (excelPath.equals("")) {
                result.put("result", "0");
                result.put("msg", "Excel生成失败，请重试");
                return result;
            } else {
                session.setAttribute("excelPath", excelPath);
                session.setAttribute("date", date);
                response.sendRedirect("http://localhost:8082/downloadExampleExcel");
                return result;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean forceLogOut(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        boolean status;
        try {
            if (session == null) {
                status = false;
                return status;
            }
            if (!session.getId().equals(accountRepository.findByStuempno((String) session.getAttribute("no")).get(0).getSessionid())) {
                status = true;
                session.invalidate();
            } else {
                status = false;
            }
        } catch (Exception e) {
//        } catch (NullPointerException e) {
            status = true;
            session.invalidate();
            return status;
        }
        return status;
    }
}
